package com.house365.core.view.pulltorefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.house365.core.view.pulltorefresh.internal.EmptyViewMethodAccessor;
/**
 * 
 * @modifier Rick Ping
 *
 */
public class PullToRefreshListView extends PullToRefreshAdapterViewBase<ListView> {

//	private BaseCacheListAdapter listadapter;
	class InternalListView extends ListView implements EmptyViewMethodAccessor {

		 // 滑动距离及坐标
        private float xDistance, yDistance, xLast, yLast;
        
		public InternalListView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		@Override
		public void setAdapter(ListAdapter adapter) {

			super.setAdapter(adapter);
		}

		@Override
		public void setEmptyView(View emptyView) {
			PullToRefreshListView.this.setEmptyView(emptyView);
		}

		@Override
		public void setEmptyViewInternal(View emptyView) {
			super.setEmptyView(emptyView);
		}

		public ContextMenuInfo getContextMenuInfo() {
			return super.getContextMenuInfo();
		}
		
		 @Override
	        public boolean onInterceptTouchEvent(MotionEvent ev) {
	                switch (ev.getAction()) {
	                case MotionEvent.ACTION_DOWN:
	                        xDistance = yDistance = 0f;
	                        xLast = ev.getX();
	                        yLast = ev.getY();
	                        break;
	                case MotionEvent.ACTION_MOVE:
	                        final float curX = ev.getX();
	                        final float curY = ev.getY();

	                        xDistance += Math.abs(curX - xLast);
	                        yDistance += Math.abs(curY - yLast);
	                        xLast = curX;
	                        yLast = curY;

	                        if (xDistance > yDistance) {
	                                return false;   //表示向下传递事件
	                        }
	                }

	                return super.onInterceptTouchEvent(ev);
	        }
	}

	public PullToRefreshListView(Context context) {
		super(context);
		setDisableScrollingWhileRefreshing(false);
	}

	public PullToRefreshListView(Context context, int mode) {
		super(context, mode);
		setDisableScrollingWhileRefreshing(false);
	}

	public PullToRefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setDisableScrollingWhileRefreshing(false);
	}

	@Override
	public ContextMenuInfo getContextMenuInfo() {
		return ((InternalListView) getRefreshableView()).getContextMenuInfo();
	}

	@Override
	protected final ListView createRefreshableView(Context context, AttributeSet attrs) {
		ListView lv = new InternalListView(context, attrs);

		// Set it to this so it can be used in ListActivity/ListFragment
		lv.setId(android.R.id.list);
	

		return lv;
	}
	public void onRefreshComplete(CharSequence lastUpdated) {
		mHeaderLayout.setLastUpdate(lastUpdated);
		 super.onRefreshComplete();
		 
	}
	public void setFooterViewVisible(int visibility){
		if(visibility == View.GONE){
			this.removeView(mFooterLayout);
			mCurrentMode = mMode = MODE_PULL_DOWN_TO_REFRESH;
			setPadding(0, -mHeaderHeight, 0,0);
		}else{
			mMode = MODE_BOTH;
			if(this.mFooterLayout != null){
			  this.removeView(mFooterLayout);
			  this.addView(mFooterLayout);
			}
			setPadding(0, -mHeaderHeight, 0, -mHeaderHeight);
			
		}
	}
	public void setAdapter(ListAdapter adapter){
		/**
		if(adapter instanceof BaseCacheListAdapter){
			listadapter = (BaseCacheListAdapter)adapter;
			getActureListView().setOnScrollListener(new OnScrollListener() {
				
				@Override
				public void onScrollStateChanged(AbsListView view, int scrollState) {
					switch (scrollState) {
			        case OnScrollListener.SCROLL_STATE_IDLE:
//			        	isScrolling= false;
			        	listadapter.onScrollIdle(getActureListView().getFirstVisiblePosition(), getActureListView().getLastVisiblePosition());
			            break;
			        case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			        	listadapter.setHasFirstScrolled(true);
//			        	listadapter.setIsScrolling(isScrolling);
//			        	isScrolling= true;     
			            break;
			        case OnScrollListener.SCROLL_STATE_FLING:
			        	listadapter.setHasFirstScrolled(true);
//			        	listadapter.setIsScrolling(isScrolling);
//			        	isScrolling= true;
			            break;
					}
					
				}
				
				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
						int visibleItemCount, int totalItemCount) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		*/
		getRefreshableView().setAdapter(adapter);
	}
//	public void setAdapterIsScrolling(boolean scrolling){
//		if(listadapter!= null){
//			listadapter.seti
//		}
//	}
	public void setOnItemClickListener(AdapterView.OnItemClickListener listener){
		getRefreshableView().setOnItemClickListener(listener);
	}
	public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener){
		getRefreshableView().setOnItemLongClickListener(listener);
	}
	public void showRefreshView(){
		setHeaderRefreshingInternal(true);
	};
	public ListView getActureListView(){
		return getRefreshableView();
	}
}